const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    aJumpRopecAtions: [{
        value: 1,
        isSelect: false,
        select: {
          title: '1分钟跳绳',
          id: 1,

        },
        list: [{
          id: 1,
          title: '1分钟跳绳'
        }, {
          id: 2,
          title: '跳绳总数'
        }]
      },
      {
        value: 1,
        isSelect: false,

        select: {
          title: '今日',
          id: 1,

        },
        list: [{
          id: 1,
          title: '今日'
        }, {
          id: 2,
          title: '本周'
        }, {
          id: 3,
          title: '本月'
        }, {
          id: 4,
          title: '本年'
        }]
      },
    ],

    userData: {},
    myQuantity: 0,
    myRank: 0,

    dataList: [],
    totalPage: 0,
    pageNo: 1,
    pageSize: 10,
    isLoading: false,
    allloaded: false,
    isRefresh: 0,
    time: 1, //时间：1今日,2本周,3本月,4本年
    type: 1 //类型：1:1分钟跳绳,2:跳绳总数

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "排行榜"
    })
    const userInfo = wx.getStorageSync('userData');
    const stuIndex = wx.getStorageSync('stuIndex');
    const data = userInfo.list[stuIndex]
    this.setData({
      userData: data,
      time: 1,
      type: 1
    })
    // this.showAction()
    this.getData()
  },


  //下拉
  didClickPull(e) {
    this.setData({
      aJumpRopecAtions: e.detail,
      pageNo: 1,
      dataList: [],
    })
    this.setData({
      allloaded: true
    })
    this.getData()
  },

  getData() {
    var data = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
      studentId: this.data.userData.studentId,
      time: this.data.aJumpRopecAtions[1].select.id, //时间：1今日,2本周,3本月,4本年
      type: this.data.aJumpRopecAtions[0].select.id //类型：1:1分钟跳绳,2:跳绳总数
    }
    console.log("排行榜=" + JSON.stringify(data));
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    this.setData({
      isLoading: true
    })
    var that = this;
    app.api.skipRopeRank(data).then(rest => {
        wx.hideLoading()
        this.setData({
          isLoading: false,
          allloaded: true

        })
        console.log('排行榜返回=' + JSON.stringify(rest))
        if (rest && rest.success) {

          var myDataList = [...that.data.dataList, ...rest.data.rankList.records]
          var vTotal = rest.data.rankList.total
          if (vTotal == 0) {
            myDataList = []
          }
          that.setData({
            dataList: myDataList,
            myQuantity: rest.data.myQuantity,
            myRank: rest.data.myRank,
            totalPage: vTotal
          })


          // var vst  =  that.data.switchList; 
          // var dataList = [...vst[that.data.selectIndex].data.list,...rest.data.list] 
          // vst[that.data.selectIndex].data =rest.data 
          // vst[that.data.selectIndex].data.list = dataList 

          // that.setData({ 
          //   switchList:vst, 
          //   totalPage:rest.data.totalCount 
          // }) 
          // console.log('列表个数数据='+this.data.switchList[that.data.selectIndex].data.list.length) 
          //console.log('列表数据='+JSON.stringify(this.data.switchList)) 
        } else {
          wx.showToast({
            title: rest.errMsg || '网络异常,请重试！',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }

      })
      .catch(r => {
        console.error(r)
        wx.showToast({
          title: '网络异常,请重试！',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      })
  },



  clickRules() {
    const desc = "1、跳绳总数排行榜：对全部用户的跳绳总个数成绩进行排名，跳绳总数排行榜显示前1000名。\r\n2、1分钟跳绳排行榜：对全部用户1分钟倒计时跳绳的个数成绩进行排名，1分钟跳绳排行榜显示前500名。\r\n3、完成任意跳绳后，当您的成绩超过“上榜基准分”时，将自动根据成绩高低参加以上排名。\r\n4、排行榜每次24点第一次更新，后续每30分钟更新一次。"
    wx.showModal({
      title: '规则',
      content: desc,
      showCancel: false,
      confirmText: '知道了',
      success: function (res) {
        if (res.confirm) {

        };
      }
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },

  onSelect(event) {
    console.log(event.detail);
  },

  showAction() {
    var action = this.selectComponent("#action-sheet");
    action.onShowSingelAlert(this.data.aJumpRopecAtions, 3);
  },
  actionSheetIndex(e) {

  },






  onPullDownRefresh() {
    this.setData({
      pageNo: 1,
      dataList: [],
      totalPage: 0
    })
    //导航条加载动画
    //wx.showNavigationBarLoading()
    this.getData(() => {
      //wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('触底事件')
    if (this.data.pageSize * this.data.pageNo >= this.data.totalPage) {
      return
    }

    if (this.data.isLoading) {
      return
    }

    this.setData({
      pageNo: this.data.pageNo + 1
    })
    this.getData()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})